package com.mcxx.modules.report.controller;

import com.mcxx.modules.report.service.ReportService;
import com.mcxx.modules.statistics.entity.FundPayReportCompare;
import com.mcxx.result.BaseResult;
import com.mcxx.service.DefaultRestService;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;


@Slf4j
@Validated
@Api(tags="报表生成控制类")
@RestService({"report/v2"})
public class ReportController extends DefaultRestService {
    @Autowired
    private ReportService service;


    @PostMapping("generateReport")
    @ApiOperation(value = "生成报表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaCodes", value = "地区:12位地区码,多个逗号分隔", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "months", value = "年月:2021年6月-202106,多个逗号分隔", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "itemSetIds", value = "综合统计表ID集合,多个逗号分隔", required = true, paramType = "query", dataType = "String"),
    })
    public void generateReport(@RequestParam(required = false) String areaCodes,
                               @RequestParam(required = false) String months,
                               @RequestParam(required = false) String itemSetIds) {
        log.info("开始生成报表，参数是：{},{},{}", areaCodes, months, itemSetIds);
        BaseResult<Boolean> result = service.generateReport(areaCodes, months, itemSetIds);
        setData(result);
        log.info("完成生成报表，结果是：{}", result);
    }


    @PostMapping("checkReportByFund")
    @ApiOperation(value = "根据名册检测报表金额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaCode", value = "地区:12位地区码", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "month", value = "年月:2021年6月-202106", required = true, paramType = "query", dataType = "String"),
    })
    public void checkReportByFund(@RequestParam(required = false) String areaCode,
                                  @RequestParam(required = false) String month) {
        log.info("开始生成报表，参数是：{},{}", areaCode, month);
        BaseResult<Boolean> result = service.checkReportByFund(areaCode, month);
        setData(result);
        log.info("完成生成报表，结果是：{}", result);
    }


    @PostMapping("generateReportSingleItem")
    @ApiOperation(value = "生成报表的单个指标数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaCodes", value = "地区:12位地区码,多个逗号分隔", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "months", value = "年月:2021年6月-202106,多个逗号分隔", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "itemSetId", value = "综合统计表ID集合", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "item", value = "综合统计表指标", required = true, paramType = "query", dataType = "String"),
    })
    public void generateReportSingleItem(@RequestParam(required = false) String areaCodes,
                                         @RequestParam(required = false) String months,
                                         @RequestParam(required = false) String itemSetId,
                                         @RequestParam(required = false) String item) {
        log.info("开始生成单个报表指标，参数是：{},{},{},{}", areaCodes, months, itemSetId, item);
        BaseResult<Boolean> result = service.generateReportSingleItem(areaCodes, months, itemSetId, item);
        setData(result);
        log.info("完成生成单个报表指标，结果是：{}", result);
    }


    @GetMapping("/fundPayWithReportCompare")
    @ApiOperation(value = "报表与名册差异性数据比对")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaCode", value = "地区:12位地区码", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "month", value = "年月:202106", paramType = "query", dataType = "String"),
    })
    public void fundPayWithReportCompare(HttpServletResponse response,
                                         @RequestParam(required = false) String areaCode,
                                         @RequestParam(required = false) String month) {

        log.info("开始比对报表与名册数据，参数是：{},{}", areaCode, month);
        Map<String, List<FundPayReportCompare>> result = service.fundPayWithReportCompare(areaCode, month);


        String[] title = {"区划编码", "区划名称", "报表名称", "报表项名称", "错误描述"};

        XSSFWorkbook workbook = new XSSFWorkbook();
        Set<String> keySet = result.keySet();
        Iterator<String> iterator = keySet.iterator();
        int i = 0;
        while (iterator.hasNext()) {
            String key = iterator.next();
            List<FundPayReportCompare> list = result.get(key);
            service.generateExcel(workbook, i, key, title, list);
            i++;
        }

        OutputStream output;
        try {
            output = response.getOutputStream();

            response.reset();

            String name = service.getReportName(areaCode, month);

            response.setHeader("Content-disposition", "attachment;filename=" + new String((name + "报表与名册差异性数据比对").getBytes(), "iso-8859-1") + ".xls");

            response.setContentType("application/vnd.ms-excel");

            workbook.write(output);

            output.close();
            log.info("完成比对报表与名册数据，结果是：{}", result);

        } catch (IOException e) {
            log.error("excel导出错误：" + e);
        }
    }
}
